home *** CD-ROM | disk | FTP | other *** search
/ BCI NET 2 / BCI NET 2.iso / archives / programming / source / tracker-4.13.lha / tracker / st_read.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-02-15  |  10.6 KB  |  469 lines

  1. /* st_read.c 
  2.     vi:ts=3 sw=3:
  3.  */
  4.  
  5. /* $Id: st_read.c,v 4.8 1995/02/08 13:14:56 espie Exp $
  6.  * $Log: st_read.c,v $
  7.  * Revision 4.8  1995/02/08  13:14:56  espie
  8.  * *** empty log message ***
  9.  *
  10.  * Revision 4.8  1995/02/08  13:14:56  espie
  11.  * *** empty log message ***
  12.  *
  13.  * Revision 4.7  1995/02/06  14:50:47  espie
  14.  * Changed sample_info.
  15.  *
  16.  * Revision 4.6  1995/02/01  17:14:54  espie
  17.  * Scaled volume.
  18.  *
  19.  * Revision 4.6  1995/02/01  17:14:54  espie
  20.  * Scaled volume.
  21.  *
  22.  * Revision 4.5  1995/02/01  16:39:04  espie
  23.  * *** empty log message ***
  24.  *
  25.  * Revision 4.0  1994/01/11  17:56:51  espie
  26.  * Use new open.
  27.  * Added lots of checks for malloc, plus count of bytes read.
  28.  * Amiga support.
  29.  * Added other commands (numerous).
  30.  * Checks for finetune ?
  31.  *
  32.  * Revision 2.16  1992/11/17  17:06:25  espie
  33.  * fix_xxx for better speed.
  34.  * Added some sample information in the dump.
  35.  * Added transpose feature.
  36.  * Feature fix: length 1 sample should be empty.
  37.  * Corrected repeat length problems concerning badly formed files,
  38.  * added signature checking for new tracker files.
  39.  * Corrected small problem with repeat being too short.
  40.  * Coded error types. More amiga specific stuff.
  41.  *
  42.  * Revision 1.17  1991/11/17  16:30:48  espie
  43.  * Rationnalized error recovery.
  44.  * There was a bug: you could try to deallocate
  45.  * stuff in no-noland. Also, strings never got
  46.  * to be freed.
  47.  * Centralized error control to error_song.
  48.  * Added a new test on length, aborts most modules now.
  49.  * Maybe should say it as well.
  50.  * Added checkpoints for early return if file too short.
  51.  * Added memory recovery and error control.
  52.  * Suppressed ! warning for bad note.
  53.  * Added note support.
  54.  * Corrected length and rep_length/rep_offset
  55.  * which are given in words and should be converted to
  56.  * bytes.
  57.  */
  58.  
  59. #include "defs.h"
  60.  
  61. #include <ctype.h>
  62. #include <assert.h>
  63.  
  64. #include "extern.h"
  65. #include "song.h"
  66. #include "channel.h"
  67.  
  68.  
  69. ID("$Id: st_read.c,v 4.8 1995/02/08 13:14:56 espie Exp $")
  70.  
  71. /***
  72.  *
  73.  * Low level st-file access routines 
  74.  *
  75.  ***/
  76.  
  77. #define MAX_LEN 50
  78. /* s = getstring(f, len):
  79.  * gets a soundtracker string from file f.
  80.  * I.e, it is a fixed length string terminated
  81.  * by a 0 if too short. Length should be
  82.  * smaller than MAX_LEN.
  83.  */
  84. LOCAL char *getstring(f, len)
  85. struct exfile *f;
  86. int len;
  87.    {
  88.    static char s[MAX_LEN];
  89.    char *new;
  90.    int i;
  91.         
  92.    assert(len < MAX_LEN);
  93.    for (i = 0; i < len; i++)
  94.       s[MIN(i, MAX_LEN - 1)] = getc_file(f);
  95.    s[MIN(len, MAX_LEN - 1)] = '\0';
  96.    new = malloc(strlen(s)+1);
  97.    if (!new) 
  98.       return NULL;
  99.  
  100.    return strcpy(new, s);
  101.    }
  102.  
  103. /* byteskip(f, len)
  104.  * same as fseek, xcpt it works on stdin
  105.  */
  106. LOCAL void byteskip(f, len)
  107. struct exfile *f;
  108. int len;
  109.    {
  110.    int i;
  111.  
  112.    for (i = 0; i < len; i++)
  113.       getc_file(f);
  114.    }
  115.  
  116. /* v = getushort(f)
  117.  * reads an unsigned short from f
  118.  */
  119. LOCAL int getushort(f)
  120. struct exfile *f;
  121.    {
  122.    int i;
  123.  
  124.       /* order dependent !!! */
  125.    i = getc_file(f) << 8;
  126.    return i | getc_file(f);
  127.    }
  128.  
  129.  
  130. /* info = fill_sample_info(f):
  131.  * allocate and 
  132.  * fill sample info with the information at current position of
  133.  * file f. Allocate memory for storing the sample, also.
  134.  * fill_sample_info is guaranteed to give you an accurate snapshot
  135.  * of what sample should be like. In particular, length, rp_length,
  136.  * start, rp_start, fix_length, fix_rp_length will have the values
  137.  * you can expect if part of the sample is missing.
  138.  */
  139. LOCAL struct sample_info *fill_sample_info(f)
  140. struct exfile *f;
  141.    {
  142.     struct sample_info *info;
  143.     int j;
  144.     static int color=1;
  145.  
  146.     info = malloc(sizeof(struct sample_info));
  147.     if (!info)
  148.         {
  149.         error = OUT_OF_MEM;
  150.         return NULL;
  151.         }
  152.     for (j = 0; j <= MAX_VOLUME; j++)        /* note <= not a bug */
  153.         info->volume_lookup[j] = 256 * j;
  154.     
  155.     info->finetune = 0;
  156.     info->name = NULL;
  157.     info->length = 0;
  158.     info->start = NULL;
  159.     info->rp_start = NULL;
  160.     info->fix_length = 0;
  161.     info->fix_rp_length = 0;
  162.  
  163.    info->name = getstring(f, SAMPLENAME_MAXLENGTH);
  164.    if (!info->name)
  165.       {
  166.       error = OUT_OF_MEM;
  167.       return NULL;
  168.       }
  169.    info->length = getushort(f);
  170.    info->finetune = getc_file(f);
  171.    if (info->finetune > 15)
  172.       info->finetune = 0;
  173.    info->volume = getc_file(f);
  174.    info->volume = MIN(info->volume, MAX_VOLUME);
  175.    info->rp_offset = getushort(f);
  176.    info->rp_length = getushort(f);
  177.  
  178.    /* the next check is for old modules for which
  179.     * the sample data types are a bit confused, so
  180.     * that what we were expecting to be #words is #bytes.
  181.     */
  182.       /* not sure I understand the -1 myself, though it's
  183.        * necessary to play kawai-k1 correctly 
  184.        */
  185.    if (info->rp_length + info->rp_offset - 1 > info->length)
  186.       info->rp_offset /= 2;
  187.     
  188.    if (info->rp_length + info->rp_offset > info->length)
  189.       info->rp_length = info->length - info->rp_offset;
  190.  
  191.    info->length *= 2;
  192.    info->rp_offset *= 2;
  193.    info->rp_length *= 2;
  194.       /* in all logic, a 2-sized sample could exist,
  195.        * but this is not the case, and even so, some
  196.        * trackers output empty instruments as being 2-sized.
  197.        */
  198.    if (info->length <= 2)
  199.       {
  200.         free(info);
  201.         return empty_sample();
  202.       }
  203.    else
  204.       {
  205.             /* add one byte for resampling */
  206.       info->start = (SAMPLE *)alloc_sample(info->length + 1);
  207.       if (!info->start)
  208.          {
  209.          error = OUT_OF_MEM;
  210.          return NULL;
  211.          }
  212.  
  213.       if (info->rp_length > 2)
  214.          info->rp_start = info->start + info->rp_offset;
  215.       else
  216.          {
  217.          info->rp_start = NULL;
  218.          info->rp_length = 0;
  219.          }
  220.         info->color = color++;
  221.         if (color > 7) color = 1;
  222.       }
  223.  
  224.    if (info->length > MAX_SAMPLE_LENGTH)
  225.       error = CORRUPT_FILE;
  226.    info->fix_length = int_to_fix(info->length);
  227.    info->fix_rp_length = int_to_fix(info->rp_length);
  228.     return info;
  229.    }
  230.  
  231. LOCAL void fill_song_info(info, f)
  232. struct song_info *info;
  233. struct exfile *f;
  234.    {
  235.    int i;
  236.    int p;
  237.  
  238.    info->length = getc_file(f);
  239.    getc_file(f);
  240.    info->maxpat = -1;
  241.    for (i = 0; i < NUMBER_PATTERNS; i++)
  242.       {
  243.       p = getc_file(f);
  244.       if (p >= NUMBER_PATTERNS)
  245.          p = 0;
  246.       if (p > info->maxpat)
  247.          info->maxpat = p;
  248.       info->patnumber[i] = p;
  249.       }
  250.    info->maxpat++;
  251.    if (info->maxpat == 0 || info->length == 0)
  252.       error = CORRUPT_FILE;
  253.    }
  254.  
  255. LOCAL void fill_event(e, f)
  256. struct event *e;
  257. struct exfile *f;
  258.    {
  259.    int a, b, c, d;
  260.  
  261.    a = getc_file(f);
  262.    b = getc_file(f);
  263.    c = getc_file(f);
  264.    d = getc_file(f);
  265.    e->sample_number = (a & 0x10) | (c >> 4);
  266.    e->effect = c & 0xf;
  267.    e->parameters = d;
  268.    if (e->effect == EFF_EXTENDED)
  269.       {
  270.       e->effect = EXT_BASE + HI(e->parameters);
  271.       e->parameters = LOW(e->parameters);
  272.       }
  273.    if (e->effect == 0)
  274.       e->effect = e->parameters ? EFF_ARPEGGIO : EFF_NONE;
  275.    if (e->effect == EFF_SKIP)
  276.       e->parameters = HI(e->parameters) * 10 + LOW(e->parameters);
  277.    e->pitch = ( (a & 15) << 8 ) | b;
  278.    e->note = find_note(e->pitch);
  279.    }
  280.  
  281. LOCAL void fill_pattern(pattern, f)
  282. struct block *pattern;
  283. struct exfile *f;
  284.    {
  285.    int i, j;
  286.  
  287.    for (i = 0; i < BLOCK_LENGTH; i++)
  288.       for (j = 0; j < NUMBER_TRACKS; j++)
  289.          fill_event(&(pattern->e[j][i]), f);
  290.    }
  291.  
  292.  
  293. LOCAL void read_sample(info, f)
  294. struct sample_info *info;
  295. struct exfile *f;
  296.    {
  297.    if (info)
  298.       obtain_sample(info->start, info->length, f);
  299.    }
  300.  
  301.  
  302.  
  303.  
  304. /***
  305.  *
  306.  *  new_song: allocates a new structure for a song.
  307.  *  clears each and every field as appropriate.
  308.  *
  309.  ***/
  310. LOCAL struct song *new_song()
  311.    {
  312.    struct song *new;
  313.     int i;
  314.  
  315.    new = (struct song *)malloc(sizeof(struct song));
  316.    if (!new) 
  317.       {
  318.       error = OUT_OF_MEM;
  319.       return NULL;
  320.       }
  321.    new->title = NULL;
  322.    new->info.length = 0;
  323.    new->info.maxpat = -1;
  324.    new->info.transpose = 0;
  325.    new->info.pblocks = NULL;
  326.     for (i = 1; i < NUMBER_SAMPLES; i++)
  327.         new->samples[i] = empty_sample();
  328.    return new;
  329.    }
  330.  
  331. /* release_song(song): gives back all memory 
  332.  * occupied by song. Assume that each structure
  333.  * has been correctly allocated by a call to the
  334.  * corresponding new_XXX function.
  335.  */
  336. void release_song(song)
  337. struct song *song;
  338.    {
  339.    int i;
  340.  
  341.    if (!song)
  342.       return;
  343.    for (i = 1; i < NUMBER_SAMPLES; i++)
  344.       {
  345.         if (song->samples[i] != empty_sample())
  346.             {
  347.             if (song->samples[i]->start)
  348.                 free_sample(song->samples[i]->start);
  349.             if (song->samples[i]->name)
  350.                 free(song->samples[i]->name);
  351.             free(song->samples[i]);
  352.             }
  353.       }
  354.    if (song->info.pblocks)
  355.       free(song->info.pblocks);
  356.    if (song->title)
  357.       free(song->title);
  358.    free(song);
  359.    }
  360.  
  361. /* error_song(song): what we should return
  362.  * if there was an error. Actually, is mostly
  363.  * useful for its side effects.
  364.  */
  365. LOCAL struct song *error_song(song)
  366. struct song *song;
  367.    {
  368.    release_song(song);
  369.    return NULL;
  370.    }
  371.  
  372. /* bad_sig(f): read the signature on file f
  373.  * and returns TRUE if it is not a known sig.
  374.  */
  375. LOCAL int bad_sig(f)
  376. struct exfile *f;
  377.    {
  378.    char a, b, c, d;
  379.  
  380.    a = getc_file(f);
  381.    b = getc_file(f);
  382.    c = getc_file(f);
  383.    d = getc_file(f);
  384.    if (a == 'M' && b == '.' && c == 'K' && d == '.')
  385.       return FALSE;
  386.    if (a == 'M' && b == '&' && c == 'K' && d == '!')
  387.       return FALSE;
  388.    if (a == 'F' && b == 'L' && c == 'T' && d == '4')
  389.       return FALSE;
  390.    return TRUE;
  391.    }
  392.  
  393. /* s = read_song(f, type): tries to read a song s
  394.  * of type NEW/OLD in file f. Might fail, i.e.,
  395.  * returns NULL if file is not a mod file of the
  396.  * correct type.
  397.  */
  398. struct song *read_song(f, type)
  399. struct exfile *f;
  400. int type;
  401.    {
  402.    struct song *song;
  403.    int i;
  404.    int ninstr;
  405.  
  406.    error = NONE;
  407.  
  408.    if (type == NEW || type == NEW_NO_CHECK)
  409.       ninstr = 31;
  410.    else
  411.       ninstr = 15;
  412.  
  413.    song = new_song();
  414.    if (!song)
  415.       return error_song(song);
  416.    song->title = getstring(f, TITLE_MAXLENGTH);
  417.    if (error != NONE)
  418.       return error_song(song);
  419.  
  420.    for (i = 1; i <= ninstr; i++)
  421.       {
  422.         song->samples[i] = fill_sample_info(f);
  423.       if (error != NONE)
  424.          return error_song(song);
  425.       }
  426.  
  427.    fill_song_info(&song->info, f);
  428.  
  429.    if (error != NONE)
  430.       return error_song(song);
  431.  
  432.    if (type == NEW && bad_sig(f))
  433.       return error_song(song);
  434.  
  435.    if (type == NEW_NO_CHECK)
  436.       byteskip(f, 4);
  437.         
  438.  
  439.    song->info.pblocks = (struct block *)
  440.       malloc(sizeof(struct block) * song->info.maxpat);
  441.    if (!song->info.pblocks)
  442.       {
  443.       error = OUT_OF_MEM;
  444.       return error_song(song);
  445.       }
  446.    for (i = 0; i < song->info.maxpat; i++)
  447.       {
  448.       fill_pattern(song->info.pblocks + i, f);
  449.       if (error != NONE)
  450.          return error_song(song);
  451.       }
  452.          /* future code... */
  453.    song->samples_start = tell_file(f);
  454.  
  455. #if 0
  456.    if (feof(f))
  457.       for (i = 1; i <= ninstr; i++)
  458.          find_sample(song->samples[i]);
  459.    else
  460. #endif
  461.       for (i = 1; i <= ninstr; i++)
  462.          read_sample(song->samples[i], f);
  463.     
  464.    if (error != NONE)
  465.       return error_song(song);
  466.    return song;
  467.    }
  468.  
  469.